<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;


class CateController extends Controller
{
    public function getAdd()
    {

        $cates = DB::table('cates')->get();
        
        // dd($cates);
        return view('admin.cate.add',['cates'=>$cates]);
    }
    public function postInsert(Request $request)
    {
        // dd(222);
        //表单验证
        //获取数据
        $data = $request->except(['_token']);
        // dd($data);
        //顶级分类
        if($data['pid'] == '0') {
            $data['path'] = '0';
        }else{
            //读取父级分类信息
            $p = DB::table('cates')->where('id','=',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->id;
            // dd($data);
        }

        //写入数据库
        $res = DB::table('cates')->insert($data);
        if($res){
            // echo 'ok';
            return redirect('/cate/list')->with('info','添加成功');
        }else{
            // echo 'error';
            return back()->with('info','添加失败');
        }
    }
    public function getList(Request $request)
    {
        // $keyword = $request->input('keyword');
        // if(empty($keyword)){


        // // dd(222);
        // $cates = DB::table('cates')->orderBy('id','desc')->paginate($request->input('num',10));
        // }else{
        //     $user = DB::table('cates')
        //     ->where('name','like','%'.$keyword.'%')
        //     ->paginate($request->input('num',10))->get();
        // }
        // // dd($user);
        // return view('admin.cate.list',['cates'=>$cates,'request'=>$request]);
         $cates = DB::table('cates')
            ->select(DB::raw('*, concat(path,",",id) as paths')) // raw 生 (原生的意思) 
            ->orderBy('paths')
            ->where(function($query)use($request){
            //检测是否传递keyword
            $keyword = $request->input('keyword');
            if($keyword) {
                $query->where('name','like','%'.$keyword.'%');                
            }
        })->get();

         //遍历
        foreach ($cates as $key => $value) {
            //检测当前的分类的等级
            $data = explode(',', $value->path);
            //统计
            $total = count($data)-1;
            //添加路径分隔  |-----
            $value->name = str_repeat('|-----', $total).$value->name;
        }

        //分配数据 解析模板
        return view('admin.cate.list', ['cates'=>$cates,'request'=>$request]);

    }

    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        //读取数据库
        $cate = DB::table('cates')->where('id','=',$id)->first();
        // dd($user);
        $cates = DB::table('cates')->get();
        return view('admin.cate.edit',['cate'=>$cate,'cates'=>$cates]);
    }

    //更新操作
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        $data = $request->except(['_token','id']);
        

        //顶级分类
        if($data['pid'] == '0') {
            $data['path'] = '0';
        }else{
            //读取父级分类信息
            $p = DB::table('cates')->where('id','=',$data['pid'])->first();
            $data['path'] = $p->path.','.$p->id;
            // dd($data);
        }

        $res = DB::table('cates')->where('id',$request->input('id'))->update($data);
        if($res){
            // echo 'ok';
            return redirect('/cate/list')->with('info','更新成功');
        }else{
            // echo 'fail';
            return back()->with('info','更新失败');
        }
   }

   public function upload($request)
   {
        if($request->hasFile('profile')){
            $fileName = time().rand(100000,999999);
            $suffix = $request->file('profile')->getClientOriginalExtension();
            // dd($suffix);
            $dir = './uploads'.date('Ymd');

            $request->file('profile')->move($dir,$fileName.'.'.$suffix);
            //拼接路径
            $data['profile'] = trim($dir.'/'.$fileName.'.'.$suffix,'.');
            return $data['profile'];
        }
        return null;
   }

   public function getDelete(Request $request)
   {
        $id = $request->input('id');
        //删除图片
        $info = DB::table('cates')->where('id',$id)->first();
      
        $res = DB::table('cates')->where('id',$id)->delete();
        if($res){
            // echo 'ok';
            return redirect('/cate/list')->with('info','删除成功');
        }else{
            // echo 'fail';
            return back()->with('info','删除失败');
        }
   }

}
